﻿

using XT.Common.Services;
using HbcServerProcess.Interfaces;
using IotContract.Dtos.Servers.Process;
using IotContract.Dtos.Servers.Pushs;
using IotContract.Models.Clients.Hks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HbcServerProcess.Validates;
/// <summary>
/// 设备验证服务
/// </summary>
public class EquipValidateService : IEquipValidateService
{
    private readonly ILogService _logService;
    public EquipValidateService(ILogService logService)
    {
        _logService= logService;
    }
    /// <summary>
    /// 验证物流线
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public bool ValidateCV(CvUnitTagDto data)
    {
        if (data == null)
        {
            return false;
        }
        // 不是实时数据
        if (!data.IsRealTime())
        {
            _logService.LogError($"{data.ToString()} is not realTime data,validate error");
            return false;
        }

        // 存在报警
        if (data.ErrorCode != 0)
        {

            return false;
        }
        // 不存在托盘，验证失败
        if (!data.IsExistTray1)
        {
            return false;
        }
        return true;


    }
    /// <summary>
    /// 验证堆垛机
    /// </summary>
    /// <param name="sc"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public bool ValidateStacker(ScUnitDto data)
    {
        if (data == null)
        {
            return false;
        }
        // 不是实时数据
        if (!data.IsRealTime())
        {
            _logService.LogError($"{data.ToString()} is not realTime data,validate error");
            return false;
        }

        // 存在报警
        if (data.ErrorCode != 0)
        {

            return false;
        }
        // 未联机
        if (data.SC_State == 0)
        {
            return false;
        }
        return true;
    }

    /// <summary>
    /// 验证库位是否存在
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public bool ValidateWrite(HkWriteInstruct data, PushHkOcvFmtDto group, PushHkOcvFmtDto group2 = null)
    {
        if (!group.Connection)
        {
            return false;
        }
        // 入库预约
        if (data.Command == "0102")
        {
            var stages = group.Stages;
            var stage = stages.FirstOrDefault(x => x.StageNo == data.Stage.ToString());

            // 手动和非可预约 异常

            if (stage == null || stage.StageMode == "1" || stage.StageStatus == "5")
            {
                return false;
            }

            if (data.Stage2 > 0 && group2 != null)
            {
                var stages2 = group2.Stages;

                var stage2 = stages2.FirstOrDefault(x => x.StageNo == data.Stage2.ToString());

                if (stage2 == null || stage2.StageMode == "1" || stage.StageStatus == "5")
                {
                    return false;
                }

            }


        }
        // 移库预约
        else if (data.Command == "0110")
        {
            var stages = group.Stages;
            var stage = stages.FirstOrDefault(x => x.StageNo == data.Stage.ToString());

            // 手动和非可预约 异常

            if (stage == null || stage.StageMode == "1")
            {
                return false;
            }


        }
        // 请求结果
        else if (data.Command == "0105")
        {
            var stages = group.Stages;
            var stage = stages.FirstOrDefault(x => x.StageNo == data.Stage.ToString());
            if (stage == null || stage.StageStatus != "4" || stage.StageMode == "1")
            {
                return false;
            }

            if (data.Stage2 > 0)
            {
                var stages2 = group2.Stages;

                var stage2 = stages2.FirstOrDefault(x => x.StageNo == data.Stage2.ToString());

                if (stage2 == null || stage2.StageStatus != "4" || stage2.StageMode == "1")
                {
                    return false;
                }

            }
        }

        return true;

    }


}
